package com.example.prison.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.CarTrafficRecord;
import com.example.prison.service.CarTrafficRecordService;
import com.example.prison.service.SystemLogService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@Slf4j
@Api(tags = "车辆通行记录")
@RestController
@RequestMapping("/api")
public class CarTrafficRecordController {

    @Autowired
    private CarTrafficRecordService carTrafficRecordService;

    @Autowired
    private SystemLogService systemLogService;

    @ApiOperation("获取车辆通行记录列表")
    @GetMapping("/getCarTrafficRecord")
    public Response<PageInfo<CarTrafficRecord>> getCarTrafficRecord(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                    @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                                    @Parameter(description = "车牌号") @RequestParam(required = false,value = "number") String number,
                                                                    @Parameter(description = "开始时间") @RequestParam(required = false,value = "beginDate", defaultValue = "0000-00-00 00:00") String beginDate,
                                                                    @Parameter(description = "结束时间") @RequestParam(required = false,value = "endDate", defaultValue = "9999-12-31 23:59") String endDate) {
        PageInfo<CarTrafficRecord> pageInfo = carTrafficRecordService.get(pageNumber, pageSize, number, beginDate, endDate);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("新增车辆通行记录")
    @PostMapping("/addCarTrafficRecord")
    public Response<?> addCarTrafficRecord(@RequestBody CarTrafficRecord record) {
        int result = carTrafficRecordService.add(record);
        log.info("新增车辆通行记录" + ", 数据：" + JSON.toJSONString(record));
        systemLogService.add("新增车辆通行记录");
        return Response.success("添加成功！");
    }

    @ApiOperation("删除车辆通行记录")
    @PostMapping("/deleteCarTrafficRecord")
    public Response<?> deleteCarTrafficRecord(@Parameter(description = "ID") @RequestParam Integer[] ids) {
        Integer[] res = {};
        for (Integer id : ids) {
            if (ObjectUtils.isEmpty(carTrafficRecordService.getById(id))) {
                res = Arrays.copyOf(res, res.length + 1);
                res[res.length - 1] = id;
            }
        }
        if (ObjectUtils.isNotEmpty(res)) {
            return Response.info("id为" + Arrays.toString(res) + "车辆通行记录不存在！", Arrays.toString(res));
        }
        carTrafficRecordService.delete(ids);
        log.info("删除车辆通行记录");
        systemLogService.add("删除车辆通行记录");
        return Response.success("删除成功！");
    }

    @ApiOperation("依据id获取车辆通行记录信息")
    @GetMapping("/getCarTrafficRecordById")
    public Response<CarTrafficRecord> getCarTrafficRecordById(@Parameter(description = "ID") @RequestParam Integer deviceId) {
        CarTrafficRecord CarTrafficRecord = carTrafficRecordService.getById(deviceId);
        if (ObjectUtils.isEmpty(CarTrafficRecord)) {
            return Response.error("此id不存在！");
        }
        return Response.success("获取成功！", CarTrafficRecord);
    }

}
